package com.ruoyi.web.controller.module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.location.City;
import com.ruoyi.system.domain.module.location.District;
import com.ruoyi.system.domain.module.location.Province;
import com.ruoyi.system.domain.module.dto.AreaTreeSelect;
import com.ruoyi.system.service.module.location.CityService;
import com.ruoyi.system.service.module.location.DistrictService;
import com.ruoyi.system.service.module.location.ProvinceService;
import com.ruoyi.web.controller.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 全国的 省份Controller
 *
 * @author mr.zhou
 * @date 2021-03-13
 */
@RestController
@RequestMapping("/module/area")
public class AreaController extends BaseController
{
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private DistrictService districtService;


    /**
     * 查询全国的 省份列表
     */
    @GetMapping("/listProv")
    public TableDataInfo<Province> listProv(Province province)
    {
        PageUtils.buildPage();
        QueryWrapper<Province> qw = new QueryWrapper<>();
        if(province.getProvinceName()!=null){
            qw.like("province_name",province.getProvinceName());
        }
        List<Province> list = provinceService.list(qw);
        return getDataTable(list);
    }

    /**
     * 查询全国的 省份列表
     */
    @GetMapping("/listProvNoPage")
    public AjaxResult listProvNoPage()
    {
        List<Province> list = provinceService.getProvinceList();
        return AjaxResult.success(list);
    }


    /**
     * 查询全国的 省份列表
     */
    @GetMapping("/getAllArea")
    public AjaxResult getAllArea(Province province)
    {
        List<AreaTreeSelect> list = provinceService.getAllArea(province);

        return AjaxResult.success(list);
    }

    /**
     *  根据省份id 查询城市列表
     */
    @GetMapping("/listCityPage")
    public TableDataInfo<City> listCityPage(City emCity)
    {
        PageUtils.buildPage();
        QueryWrapper<City> qw = new QueryWrapper<>();
        qw.eq("province_id",emCity.getProvinceId());
        List<City> list = cityService.list(qw);

        return getDataTable(list);
    }

    /**
     *  根据省份id 查询城市列表
     */
    @GetMapping("/listCity")
    public AjaxResult listCity(City emCity)
    {
        QueryWrapper<City> qw = new QueryWrapper<>();
        qw.eq("province_id",emCity.getProvinceId());
        List<City> list = cityService.list(qw);
        return AjaxResult.success(list);
    }

    /**
     * 根据城市id 查询区县列表
     */
    @GetMapping("/listDistrictPage")
    public TableDataInfo<District> listDistrictPage(District district)
    {
        PageUtils.buildPage();
        QueryWrapper<District> qw = new QueryWrapper<>();
        qw.eq("city_id",district.getCityId());
        List<District> list = districtService.list(qw);
        return getDataTable(list);
    }

    /**
     * 根据城市id 查询区县列表
     */
    @GetMapping("/listDistrict")
    public AjaxResult listDistrict(District district)
    {
        QueryWrapper<District> qw = new QueryWrapper<>();
        qw.eq("city_id",district.getCityId());
        List<District> list = districtService.list(qw);
        return AjaxResult.success(list);
    }
}
